package com.hckjsec.cloud.controller;

import java.util.List;

import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.cloud.domain.WebApplicationNews;
import com.hckjsec.cloud.service.IWebApplicationNewsService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公司网站新闻Controller
 * 
 * @author cyt
 * @date 2021-09-22
 */
@RestController
@RequestMapping("/web/internetnews")
public class WebApplicationNewsController extends BaseController
{
    @Autowired
    private IWebApplicationNewsService webApplicationNewsService;

    /**
     * 查询公司网站新闻列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebApplicationNews webApplicationNews)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webApplicationNews.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebApplicationNews> list = webApplicationNewsService.selectWebApplicationNewsList(webApplicationNews);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    /**
     * 导出公司网站新闻列表
     */
    @Log(title = "公司网站新闻", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebApplicationNews webApplicationNews)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webApplicationNews.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebApplicationNews> list = webApplicationNewsService.selectWebApplicationNewsList(webApplicationNews);
        ExcelUtil<WebApplicationNews> util = new ExcelUtil<WebApplicationNews>(WebApplicationNews.class);
        return util.exportExcel(list, "news");
    }

    /**
     * 获取公司网站新闻详细信息
     */
    @GetMapping(value = "/getnews/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webApplicationNewsService.selectWebApplicationNewsById(id));
    }

    /**
     * 新增公司网站新闻
     */
    @Log(title = "公司网站新闻", businessType = BusinessType.INSERT)
    @PostMapping("/addnews")
    public AjaxResult add(@RequestBody WebApplicationNews webApplicationNews)
    {
        int rows =0;
        if (StringUtils.isNull(webApplicationNews.getId())){
            rows=webApplicationNewsService.insertWebApplicationNews(webApplicationNews);
        }else{
            webApplicationNews.setUpdatedAt(DateUtils.parseDate(DateUtils.getTime()));
            rows=webApplicationNewsService.updateWebApplicationNews(webApplicationNews);
        }
        return toAjax(rows);
    }

    /**
     * 删除公司网站新闻
     */
    @Log(title = "公司网站新闻", businessType = BusinessType.DELETE)
	@DeleteMapping("/delnews/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webApplicationNewsService.deleteWebApplicationNewsByIds(ids));
    }
}
